package com.clz.appdesign.basic_study;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * @author chenliangzhi
 * @date 2018/6/27
 * @describe
 */

public class BookProvider extends ContentProvider {
    private Context mContext;
    private SQLiteDatabase mSQLiteDatabase;
    private static final String AUTHORITY = "com.clz.appdesign.basic_study.BookProvider";
    private static final int BOOK_URI_CODE = 0;
    private static final int USER_URI_CODE = 1;
    private static final UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);


    static {
        mUriMatcher.addURI(AUTHORITY, DBOpenHelper.BOOK_TABLE_NAME, BOOK_URI_CODE);
        mUriMatcher.addURI(AUTHORITY, DBOpenHelper.USER_TABLE_NAME, USER_URI_CODE);
    }

    private String getTableName(Uri uri) {
        String tableName = null;
        switch (mUriMatcher.match(uri)) {
            case BOOK_URI_CODE:
                tableName = DBOpenHelper.BOOK_TABLE_NAME;
                break;
            case USER_URI_CODE:
                tableName = DBOpenHelper.USER_TABLE_NAME;
                break;
            default:
                break;
        }
        return tableName;
    }

    public BookProvider() {
    }

    @Override
    public boolean onCreate() {
        mContext = getContext();
        initProviderData();
        return false;
    }

    private void initProviderData() {
        mSQLiteDatabase = new DBOpenHelper(mContext).getWritableDatabase();
        mSQLiteDatabase.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put("bookName", "数据结构");
        mSQLiteDatabase.insert(DBOpenHelper.BOOK_TABLE_NAME, null, contentValues);
        contentValues.put("bookName", "编译原理");
        mSQLiteDatabase.insert(DBOpenHelper.BOOK_TABLE_NAME, null, contentValues);
        contentValues.put("bookName", "网络原理");
        mSQLiteDatabase.insert(DBOpenHelper.BOOK_TABLE_NAME, null, contentValues);
        contentValues.clear();

        contentValues.put("userName", "张三");
        contentValues.put("sex", "男");
        mSQLiteDatabase.insert(DBOpenHelper.USER_TABLE_NAME, null, contentValues);
        contentValues.put("userName", "李四");
        contentValues.put("sex", "男");
        mSQLiteDatabase.insert(DBOpenHelper.USER_TABLE_NAME, null, contentValues);
        contentValues.put("userName", "婷婷");
        contentValues.put("sex", "女");
        mSQLiteDatabase.insert(DBOpenHelper.USER_TABLE_NAME, null, contentValues);
        mSQLiteDatabase.setTransactionSuccessful();
        mSQLiteDatabase.endTransaction();
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("Unsupported URI:" + uri);
        }
        return mSQLiteDatabase.query(tableName, projection, selection, selectionArgs, null, null, sortOrder, null);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("Unsupported URI:" + uri);
        }
        mSQLiteDatabase.insert(tableName, null, values);
        mContext.getContentResolver().notifyChange(uri, null);
        return uri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("Unsupported URI:" + uri);
        }
        final int count = mSQLiteDatabase.delete(tableName, selection, selectionArgs);
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("Unsupported URI:" + uri);
        }
        final int row = mSQLiteDatabase.update(tableName, values, selection, selectionArgs);
        if (row > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return row;
    }
}
